#include "settings.h"
#include "log.h"
#include "SDL2/SDL_scancode.h"

void initSettings()
{
    Dvars.clear();

    addDvar("WINDOW_WIDTH", 800);
    addDvar("WINDOW_HEIGHT", 600);
    addDvar("WINDOW_TITLE", "Vertical scroller project");
    addDvar("WINDOW_FULLSCREEN", false);
    addDvar("WINDOW_VSYNC", true);
    addDvar("LOG_FILE", "game.log");
    addDvar("LOG_LEVEL", LOG_DEBUG);
    addDvar("KEY_UP", SDL_SCANCODE_W);
    addDvar("KEY_LEFT", SDL_SCANCODE_A);
    addDvar("KEY_DOWN", SDL_SCANCODE_S);
    addDvar("KEY_RIGHT", SDL_SCANCODE_D);
    addDvar("KEY_FULLSCREEN", SDL_SCANCODE_F9);
    addDvar("KEY_PAUSE", SDL_SCANCODE_PAUSE);
    addDvar("KEY_SLOW", SDL_SCANCODE_LSHIFT);
    addDvar("KEY_SHOOT", SDL_SCANCODE_Z);

    log(LOG_INFO, "Loaded initial settings\n");
}

std::string getString(std::string name)
{
    if (exists(name, DVAR_STRING))
        return Dvars[name]->m_valueString;
    else
        return "";
}

int getInt(std::string name)
{
    if (exists(name, DVAR_INT))
        return Dvars[name]->m_valueInt;
    else
        return 0;
}

bool getBool(std::string name)
{
    if (exists(name, DVAR_BOOL))
        return Dvars[name]->m_valueBool;
    else
        return false;
}

void addDvar(std::string name, std::string value)
{
    Dvars.insert(std::pair<std::string, Dvar*>(name, new Dvar(value)));
}

void addDvar(std::string name, int value)
{
    Dvars.insert(std::pair<std::string, Dvar*>(name, new Dvar(value)));
}

void addDvar(std::string name, bool value)
{
    Dvars.insert(std::pair<std::string, Dvar*>(name, new Dvar(value)));
}

void editDvar(std::string name, std::string value)
{
    if (exists(name, DVAR_STRING))
        Dvars[name]->m_valueString = value;
}

void editDvar(std::string name, int value)
{
    if (exists(name, DVAR_INT))
        Dvars[name]->m_valueInt = value;
}

void editDvar(std::string name, bool value)
{
    if (exists(name, DVAR_BOOL))
        Dvars[name]->m_valueBool = value;
}

void resetDvar(std::string name)
{
    if (exists(name))
        Dvars[name]->resetValue();
}

bool exists(std::string name, int type)
{
    if (Dvars.count(name) == 0)
        return false;

    if (Dvars[name]->m_type == type)
        return true;

    return false;
}

bool exists(std::string name)
{
    if (Dvars.count(name) == 0)
        return false;

    return true;
}
